﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebChatServer.Models;

namespace WebChatServer.Controllers
{

    [HttpHeader("Access-Control-Allow-Origin", "*")]
    [HttpHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")]

    public class MemberController : ApiController
    {
        IMemberRepository _repository = new MemberRepository();
        //
        // GET: /ACMember/

        public void Options()
        {

        }

        public ChatMember Get(string id)
        {
            ChatMember member = _repository.Get(id);

            if (member == null)
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NoContent);
                throw new HttpResponseException(response);
            }
            else
                return member;
        }

        /*public  IEnumerable<FriendGroup> GetAllGroup(string id)
        {
            return _repository.GetAllGroup(id);
        } */

        public IEnumerable<ChatMember> GetAll()
        {
            return _repository.GetAll();
        }

        public bool Put(string id, Update member)
        {
            member.Id = id;
            return _repository.UpdateInfo(member);
        }

        public Update Post(Update member)
        {
            _repository.UpdateInfo(member);
            //var response = Request.CreateResponse<ACMember>(HttpStatusCode.Created, member);

            //string uri = Url.Link("DefaultApi", new { id = member.Id });
            //response.Headers.Location = new Uri(uri);

            return member;
        }


        public HttpResponseMessage DeleteMember(string id)
        {
            _repository.Remove(id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
